﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using fingerWinForm.SVM;
using fingerWinForm.Utilities;

namespace fingerWinForm.GUI
{
    public partial class CreateSvmModelWindow : Form
    {
        private String[] arrayOfParameters;
        private String pathForSVMTrainFile;
        private String testName;
        public CreateSvmModelWindow()
        {
            InitializeComponent();
        }

        public String[] readSvmParametersFromFile()
        {
            testName = chooseTestComboBox.Text;
            String path = "";
            switch (testName)
            {
                case "23 - Finger Taps":
                    if (File.Exists(MainWindow.startingPath + @"svm\finger Taps\Parameters\MaxSVMParams.csv"))
                    {
                        path = MainWindow.startingPath + @"svm\finger Taps\Parameters\MaxSVMParams.csv";
                    }
                    else
                    {
                        MessageBox.Show("There is no parameters file.\nPlease run SVM to create parameters file.","Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
                        return null;
                    }
                    break;
                case "24 - Hand Movements":
                    if (File.Exists(MainWindow.startingPath + @"svm\hand Movements\Parameters\MaxSVMParams.csv"))
                    {
                        path = MainWindow.startingPath + @"svm\hand Movements\Parameters\MaxSVMParams.csv";
                    }
                    else
                    {
                        MessageBox.Show("There is no parameters file.\nPlease run SVM to create parameters file.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return null;
                    }
                    break;
            }
            
            StreamReader parametersFile = new StreamReader(path);
            String line = parametersFile.ReadLine();
            line = parametersFile.ReadLine();
            String[] arrayOfParameters = line.Split(',');
            return arrayOfParameters;
        }

        private void chooseTestComboBox_TextChanged(object sender, EventArgs e)
        {
            gammaTextBox.Clear();
            cTextBox.Clear();
            arrayOfParameters = readSvmParametersFromFile();
            if (arrayOfParameters != null)
            {
                gammaTextBox.Text = arrayOfParameters[0];
                cTextBox.Text = arrayOfParameters[1];
            }
            else
            {
                Close();
            }
        }

        private void browseButton_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "Train File (*.train)|*.train";
            openFileDialog.ShowDialog();
            fileNameTextBox.Text = openFileDialog.FileName;
            pathForSVMTrainFile = openFileDialog.FileName;
            pathToolTip.SetToolTip(fileNameTextBox, pathForSVMTrainFile);
        }

        private void createButton_Click(object sender, EventArgs e)
        {
            if (CheckInput.isEmpty(chooseTestComboBox.Text))
            {
                MessageBox.Show("You must choose a test", "SVM", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (CheckInput.isEmpty(fileNameTextBox.Text))
            {
                MessageBox.Show("Train file is missing", "SVM", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            String fileName = "PathOfSVMModelFile.txt";
            SVMManager.runSVMtrain(Convert.ToDouble(gammaTextBox.Text),Convert.ToDouble(cTextBox.Text),pathForSVMTrainFile);
            MessageBox.Show("Model file was created", "SVM", MessageBoxButtons.OK, MessageBoxIcon.Information);
            StreamWriter file;
            String path = "";
            switch (testName)
            {
                case "23 - Finger Taps":
                    path = @"svm\finger Taps\";
                    break;

                case "24 - Hand Movements":
                    path = @"svm\hand Movements\";
                    break;
            }
            String firstLine = pathForSVMTrainFile + ".model";
            file = new StreamWriter(MainWindow.startingPath + path + fileName);
            file.WriteLine(firstLine);
            file.Close();
            Close();
        }

    }
}
